﻿#region License
// (c) Intergen.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using TextGlow.Control.Utilities;

namespace TextGlow.Control.Controls.Document
{
  public partial class TableRowControl : UserControlExtended
  {
    public TableRowControl()
    {
      InitializeComponent();
    }

    public UIElementCollection Cells
    {
      get { return CellContainer.Children; }
    }

    public TableControl Table
    {
      get
      {
        FrameworkElement parent = Parent as FrameworkElement;
        while (parent != null)
        {
          if (parent is TableControl)
            return (TableControl)parent;

          parent = parent.Parent as FrameworkElement;
        }

        return null;
      }
    }

    public double? GetRowSpanMaxHeight(int rowSpan)
    {
      return Cells
        .Select(v => (TableCellControl)v)
        .Where(c => c.RowSpan == rowSpan)
        .Max(c => (double?)c.CustomDesiredSize.Height);
    }

    protected override Size CustomMeasureOverride(Size availableSize)
    {
      CellContainer.CustomMeasure(LayoutUtils.InfiniteSize);

      Height = GetRowSpanMaxHeight(1).Value;

      Height = Table.CalculateRowHeight(Table.RowContainer.Children.IndexOf(this));

      return new Size(CellContainer.CustomDesiredSize.Width, Height);
    }

    private double CalculateRowMaxHeight()
    {
      return Cells
        .Max(c => ((TableCellControl)c).Height);
      //  //.Select(v => (TableCellControl)v)

      //double maxHei

      //foreach (TableCellControl cell in Cells)
      //{

      //}
    }

    protected override Size CustomArrangeOverride(Size finalSize)
    {
      double rowHeight = CalculateRowMaxHeight();
      if (double.IsNaN(rowHeight))
        rowHeight = finalSize.Height;

      CellContainer.CustomArrange(LayoutUtils.CreateRect(LayoutUtils.Zero, finalSize));

      return base.CustomArrangeOverride(new Size(finalSize.Width, rowHeight));
    }

    //protected override void PerformLayout()
    //{
    //  _cellContainer.AutoSize = AutoSize.Both;
    //  _cellContainer.IsHorizontal = true;
    //  _cellContainer.VerticalAlignment = VerticalAlignment.Top;
    //  _cellContainer.UpdateLayout();

    //  Width = _cellContainer.Width;
    //  Height = GetRowSpanMaxHeight(1).Value;

    //  Height = Table.CalculateRowHeight(Table.RowContainer.Children.IndexOf(this));

    //  //foreach (TableCellControl cell in _cellContainer.Children)
    //  //{
    //  //  cell.AutoSize = AutoSize.Height;
    //  //  cell.UpdateLayout();

    //  //  //FrameworkElementBase.SetLocation(cell, new Point(, 0));

    //  //  if (cell.RowSpan == 1)
    //  //  {
    //  //    Height = Math.Max(Height, cell.Height);
    //  //  }
    //  //  else
    //  //  {
    //  //    Height = Math.Max(Height, cell.Height);
    //  //    MaxHeight = Math.Max(MaxHeight, cell.Height);
    //  //  }

    //  //  Width = Math.Max(Width, cell.Width);
    //  //}
    //}
  }
}
